function lstinf$(a$,b$,c$)
  adra=1:adrb=1:blngt=len(b$):d$=""
  while adrb<=blngt
    if mid$ (a$,adra,1)=mid$ (b$,adrb,1) then:adrb+=1:end if
    adra=adra+1
    end while
  while mid$(a$,adra,1)<>left$(c$,1)
    d$=d$+mid$(a$,adra,1):adra=adra+1
    end while
  lstinf$=d$
  end function

function vldc(a$)
  t0$="!"+a$+".0.0.0."
  t1$=lstinf$(t0$,"!",".")
  t2$=lstinf$(t0$,".",".")
  t3=10^(len(t2$))
  vldc=val(t1$)+(val(t2$)/t3)
  end function

dim chfq[16],chvl[16]

finp$="_.csv"
if argc>2 then:finp$=argv(2):end if

print"56676D20 ;- vgm"
print"FFFFFF0F ;- eof"
print"60010000 ;- version"
print"00000000 ;- 999E3600 ;- sn clock ;- tirar"
print"00000000 ;- "
print"0A080000 ;- gd3"
print"30130800 ;- total samples"
print"67000000 ;- loop offset"
print"00000000 ;- "
print"3C000000 ;- rate"
print"09001000 ;- flag bits"
print"B60A7500 ;- ym2612 clock"
print"00000000 ;- "
print"4C000000 ;- vgm data offset"
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"00000000000000000000000000000000"
print"0000000000000000"
print""
print"522208 522700"
print"52B4C0 52B5C0 52B6C0"
print"53B4C0 53B5C0 53B6C0"
print"522800 522801 522802 522803 522804 522805 522806"
'print"509F 50BF 50DF 50FF"
print"523131 524119 52511F 526100 527100 528100 529100"
print"523502 52450F 52551F 526500 527500 528506 529500"
print"523975 52490F 52591F 526900 527900 528906 529900"
print"523D08 524D0F 525D1F 526D00 527D00 528D06 529D00"
print"52B136 52A51A 52A1D3 5228F1 "
print"523231 524219 52521F 526200 527200 528200 529200"
print"523602 52460F 52561F 526600 527600 528606 529600"
print"523A75 524A0F 525A1F 526A00 527A00 528A06 529A00"
print"523E08 524E0F 525E1F 526E00 527E00 528E06 529E00"
print"52B236 52A61B 52A22B 5228F2"
print"533031 534019 53501F 536000 537000 538000 539000"
print"533402 53440F 53541F 536400 537400 538406 539400"
print"533875 53480F 53581F 536800 537800 538806 539800"
print"533C08 534C0F 535C1F 536C00 537C00 538C06 539C00"
print"53B036 53A41B 53A05B 5228F4"
print"533131 534119 53511F 536100 537100 538100 539100"
print"533502 53450F 53551F 536500 537500 538506 539500"
print"533975 53490F 53591F 536900 537900 538906 539900"
print"533D08 534D0F 535D1F 536D00 537D00 538D06 539D00"
print"53B136 53A51B 53A1C5 5228F5 "
print"533231 534219 53521F 536200 537200 538200 539200 "
print"533602 53460F 53561F 536600 537600 538606 539600"
print"533A75 534A0F 535A1F 536A00 537A00 538A06 539A00"
print"533E08 534E0F 535E1F 536E00 537E00 538E06 539E00"
print"53B236 53A61C 53A23B 5228F6 61D089"
print""
print"523031 524019 52501F 526000 527000 528000 529000"
print"523402 52440F 52541F 526400 527400 528406 529400"
print"523875 52480F 52581F 526800 527800 528806 529800"
print"523C08 524C0F 525C1F 526C00 527C00 528C06 529C00 "
print"52B036 52A41A 52A084"
print"5228F0 522801 522802 522804 522805 522806"

lct=0

open finp$ for input as #1
while eof(1)=0
  file input #1,txrd$
  setcaption("b:"+str$(lct)):lct+=1
  if mid$(txrd$,1,1)="[" then:
    if mid$(txrd$,2,1)="f" then:
      chid=val(mid$(txrd$,3,2))
      chid=chid mod 6 'ym2612 has 6 channels
      chfqv=vldc(lstinf$(txrd$,":","]"))
      chfqvo=int(chfqv)
      if chfqvo<1 then:chfqvo=1:end if
      if chfqvo>8 then:chfqvo=8:end if
      chfqvf=chfqv-chfqvo
      chfqv3=int(644*2^chfqvf)
      chfqv3a=chfqv3 mod 256
      chfqv3b=int(chfqv3/256)
      chfqv5=(chfqvo*8+chfqv3b)
      chfqv2=int(26.6984611843*2^(8-chfqv))
      if chfqv2>2047 then:chfqv2=2047:end if
      if chfqv2<0 then:chfqv2=0:end if
      chchrfq$=right$("000"+hex$(chfqv2),3)
      txrgo$="52280"+right$("0"+hex$(chid),1)
      txrgo$+=" 52A4"+right$("00"+hex$(chfqv5),2)
      txrgo$+=" 52A0"+right$("00"+hex$(chfqv3a),2)
      txrgo$+=" 5228F"+right$("0"+hex$(chid),1)
      print ucase$(txrgo$)
      end if
    if mid$(txrd$,2,1)="v" then:
      chid=val(mid$(txrd$,3,2))
      chid=chid mod 6 'ym2612 has 6 channels
      chvlv=vldc(lstinf$(txrd$,":","]"))*0x10
      chchrvl$=right$("00"+hex$(chvlv),2)
      txrgo$="A0"+right$("00"+hex$(8+chid),2)+chchrvl$
      print ucase$(txrgo$)
      end if
    if mid$(txrd$,2,3)="wvb" then:
      wvblv=vldc(lstinf$(txrd$,":","]"))
      wvblv2=wvblv*44100
      wvblv3a=wvblv2 mod 65536
      wvblv3b=int(wvblv2/65536)
      wvblv3as$=ucase$(right$("0000"+hex$(wvblv3a),4))
      for ii=1 to wvblv3b:print"61FFFF":next
      print "61"+mid$(wvblv3as$,3,2)+mid$(wvblv3as$,1,2)
      end if
    end if
  wend
print:print "66":print
close #1


